home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / pistol.zip / SESSION1.DOC < prev    next >
Text File  |  1987-08-20  |  9KB  |  386 lines

  1. X> % Welcome back to the next PISTOL session!
  2. X> 
  3. X> % I shall show in this session how to create things
  4. X> % specifically, I shall show how to define CONSTANTs
  5. X> % VARIABLEs, WORDs, and "inline macros".
  6. X> 
  7. X> % CONSTANTs
  8. X> % suppose I wish to define "1K" as 1024 in decimal in
  9. X> % order to increase the readability and decrease the size
  10. X> % of the program.  Constants are an important tool to
  11. X> % simplify future modifications in a program.
  12. X> 
  13. X> 1024 '1K CONSTANT
  14. X> 
  15. X> % we can test if the definition "took" (really unnecessary)
  16. X> % but good for demonstration!
  17. X> 
  18. X> 1K =
  19. 1024
  20. X> 
  21. X> % I shall digress to the question of number bases.  We have bee
  22. X> % been using "decimal" all along.  We could change, say to
  23. X> % Hexadecimal (base 16) by the command:
  24. X> 
  25. X> HEX
  26. H> 
  27. H> % notice how the prompt is now "H>", to indicate we are
  28. H> % now conversing with PISTOL in hexadecimal.
  29. H> 
  30. H> % What is "1K" in hex?:
  31. H> 
  32. H> 1K =
  33. 400
  34. H> 
  35. H> % here is another demonstration we are in "hex":
  36. H> 
  37. H> 8 8 + =
  38. 10
  39. H> 
  40. H> % the "10" is sixteen in hex; here's another demo:
  41. H> 
  42. H> 100 2 / =
  43. 80
  44. H> 
  45. H> 100 2 /
  46. 1H> DECIMAL
  47. 1X> =
  48. 128
  49. X> % note that the prompt helps to remind us which number base
  50. X> % we are using.  The value of a CONSTANT is not changing
  51. X> % when we change number bases, but writing its value "looks"
  52. X> % different in each number base.  Let's see "1K":
  53. X> 
  54. X> BINARY
  55. B> 1K =
  56. 10000000000
  57. B> 
  58. B> OCTAL
  59. Q> 1K =
  60. 2000
  61. Q> 
  62. Q> HEX
  63. H> 1K =
  64. 400
  65. H> 
  66. H> DECIMAL
  67. X> 1K =
  68. 1024
  69. X> 
  70. X> 
  71. X> % VARIABLEs
  72. X> % It is possible to store items in named locations like
  73. X> % most other higher-level languages.  For example,
  74. X> % " X = Y + Z " is probably understood as " take what is
  75. X> % stored at location 'X'; take what is stored at location
  76. X> % 'Y' ; add them; store the result at the location 'Z' "
  77. X> 
  78. X> % We must define and allocate space for variables before
  79. X> % we can use them; we shall define the variables, Y and Z
  80. X> % with initial values of 3 and 4 initially:
  81. X> 
  82. X> 3 'Y variable
  83. X> 4 'Z VARIABLE
  84. X> 
  85. X> % to test the values of variables we use the word "?":
  86. X> 
  87. X> Y ?
  88. 3
  89. X> Z ?
  90. 4
  91. X> 
  92. X> % to simulate X=Y+Z we better define X first:
  93. X> 
  94. X> 0 'X variable
  95. X> 
  96. X> X ?
  97. 0
  98. X> 
  99. X> % Here goes:
  100. X> 
  101. X> Y W@  Z W@   +    X W!
  102. X> 
  103. X> X ?
  104. 7
  105. X> % we did it.  We can continue to use X,Y, and Z for the
  106. X> % remainder of the session.  To see what have been the
  107. X> % ten most recent definitions we can use:
  108. X> 
  109. X> TOP10
  110. 25280   X
  111. 25264   Z
  112. 25248   Y
  113. 25234   1K
  114. 25132   FINISH
  115. 25066   WRITE
  116. 25020   READ
  117. 24990   1READ
  118. 24930   DELETES
  119. 24912   REPLACE
  120. 1X> 
  121. 1X> % note this command left an item on the stack. It is
  122. 1X> % used to enable us to find the names of yet earlier
  123. 1X> % definitions:
  124. 1X> 
  125. 1X> NEXT10
  126. 24898   DELETE
  127. 24874   (DELETE)
  128. 24808   INPUT
  129. 24700   LI
  130. 24684   ARG#ERR
  131. 24644   1POSARG?
  132. 24610   LENTER
  133. 24596   MTDN
  134. 24570   OVERWRITE
  135. 24534   MTUP
  136. 1X> 
  137. 1X> NEXT10
  138. 24488   #GETLINE
  139. 24450   LDDR
  140. 24412   LDIR
  141. 24306   LFIND
  142. 24290   ILLEGLIN
  143. 24236   LISTALL
  144. 24202   NEXTLINE
  145. 24164   NEWF
  146. 24148   EOT
  147. 24132   OLDLINE^
  148. 1X> 
  149. 1X> DROP
  150. X> 
  151. X> % the DROP is for "good housekeeping".
  152. X> 
  153. X> % back to CONTANTs and VARIALEs:
  154. X> % suppose we want to do:  X = '1K' + Y  :
  155. X> 
  156. X> 1K   Y W@   +    X W!
  157. X> 
  158. X> X ?
  159. 1027
  160. X> 
  161. X> % looks correct.   It's about time I explain the words:
  162. X> % W@ and W!  The first, "word-at" is used to get the
  163. X> % contents of a location ( a VARIABLE provides a pointer
  164. X> % to the location).  The second, "word-store", is used
  165. X> % to place the item next to the top of stack into the
  166. X> % location pointed to by the top of stack.
  167. X> 
  168. X> % Let's follow the last example using the STACK utility:
  169. X> 
  170. X> STACK
  171. (0)
  172. X> 1K STACK
  173. (1)  1024
  174. 1X> Y STACK
  175. (2)  1024  25254
  176. 2X> W@ STACK
  177. (2)  1024  3
  178. 2X> + STACK
  179. (1)  1027
  180. 1X> X STACK
  181. (2)  1027  25286
  182. 2X> W! STACK
  183. (0)
  184. X> 
  185. X> X STACK
  186. (1)  25286
  187. 1X> ? STACK
  188. 1027
  189. (0)
  190. X> 
  191. X> % the ? is equivalent to the pair of words:  W@ =
  192. X> 
  193. X> % creating new WORDs....
  194. X> % suppose we didn't have or didn't know the word, "?" ,
  195. X> % we could invent it: suppose we call our word, "IS" :
  196. X> 
  197. X> 'IS :  W@ =  ;
  198. X> 
  199. X> % testing this new word:
  200. X> 
  201. X> X IS
  202. 1027
  203. X> 
  204. X> Y IS
  205. 3
  206. X> 
  207. X> Z IS
  208. 4
  209. X> % it appears to work. Once a word exists, we can define
  210. X> % newer words yet, for example:
  211. X> 
  212. X> 'XYZ? : X IS  SPACE   Y IS  SPACE  Z IS    ;
  213. X> 
  214. X> % testing:
  215. X> 
  216. X> XYZ?
  217. 1027 3 4
  218. X> 
  219. X> % further testing:
  220. X> 
  221. X> -15  X W!
  222. X> XYZ?
  223. -15 3 4
  224. X> 
  225. X> 
  226. X> % DIS-assembling words:  (very useful if you forgot how
  227. X> % you created a definition!)
  228. X> 
  229. X> 'XYZ?  DIS
  230. 'XYZ? [:]       X       IS      SPACE   Y       IS      SPACE   
  231. Z       IS      ;
  232. X> 
  233. X> 
  234. X> % Thus DIS shows us more or less the same thing as we typed.
  235. X> % more examples:
  236. X> 
  237. X> 'IS DIS
  238. 'IS [:] W@      =       ;
  239. X> 
  240. X> '? DIS
  241. '? [:]  W@      =       ;
  242. X> 
  243. X> 
  244. X> % So we see that the "?" was originally defined the same
  245. X> % way we defined "IS".
  246. X> 
  247. X> % Sometimes we wish to observe the internal action of a
  248. X> % definition, say, for debugging.  We use the utility
  249. X> % "TRACE":
  250. X> 
  251. X> 'XYZ?  TRACE
  252. 'XYZ? BEING TRACED:
  253. (0)                                             X  
  254. (1)  25286                                      IS  -15
  255. (0)                                             SPACE   
  256. (0)                                             Y  
  257. (1)  25254                                      IS  3
  258. (0)                                             SPACE   
  259. (0)                                             Z  
  260. (1)  25270                                      IS  4
  261. (0)                                             (;)
  262. TRACE COMPLETED
  263. X> 
  264. X> 
  265. X> % notice that the STACK utility is interposed in between
  266. X> % each word that makes up the definition being tested.
  267. X> % fortunately, however, the individual words, such as
  268. X> % "IS" are not also "traced"; if TRACE showed every
  269. X> % step we would have too much clutter.  It is then
  270. X> % possible to do a TRACE on a suspicious subsidiary word,
  271. X> % and so on...
  272. X> 
  273. X> % inline macros:
  274. X> % as the last topic of this session we shall show an
  275. X> % alternate way to define new words:
  276. X> % we will define "DOUBLE" in two ways:
  277. X> 
  278. X> 'DOUBLE1 : DUP + ;
  279. X> 'DOUBLE2 $: DUP + ;$
  280. X> 
  281. X> % testing each:
  282. X> 3 DOUBLE1 =
  283. 6
  284. X> 3 DOUBLE2 =
  285. 6
  286. X> 
  287. X> % they appear to behave at least similarly.  What about
  288. X> % disassembling them:
  289. X> 
  290. X> 'DOUBLE1 dis
  291. 'DOUBLE1 [:]    DUP     +       ;
  292. X> 'DOUBLE2 dis
  293. 'DOUBLE2 [$:]   DUP     +       ;
  294. X> 
  295. X> % Now I can try to illustrate how they really differ, by
  296. X> % defining QUADUPLE using both:
  297. X> 
  298. X> 'QUADUPLE :
  299.  
  300. 1X:> DOUBLE1
  301.  
  302. 1X:> DOUBLE2
  303.  
  304. 1X:> ;
  305. X> 
  306. X> % testing:
  307. X> 
  308. X> 3 quaduple =
  309. 12
  310. X> % seems reasonable (yes, I realize my spelling is bad!)
  311. X> % let's use DIS:
  312. X> 
  313. X> 'QUADUPLE DIS
  314. 'QUADUPLE [:]   DOUBLE1 DUP     +       ;
  315. X> 
  316. X> % Where did DOUBLE2 go?  We see that it was repleced by
  317. X> % " DUP + ", which is the way we defined DOUBLE2 anyway.
  318. X> 
  319. X> % the difference between using the : ; pair for defining
  320. X> % a word and the $: ;$ pair for the definition is the
  321. X> % the former creates a "subroutine" and the latter defines
  322. X> % a pattern to be substituted into whereever it will be
  323. X> % used.  The choice of which to use is that of space
  324. X> % verses speed.
  325. X> 
  326. X> % Let's use TOP10 to see what monsters we have created:
  327. X> 
  328. X> TOP10
  329. 25364   QUADUPLE
  330. 25350   DOUBLE2
  331. 25336   DOUBLE1
  332. 25310   XYZ?
  333. 25296   IS
  334. 25280   X
  335. 25264   Z
  336. 25248   Y
  337. 25234   1K
  338. 25132   FINISH
  339. 1X> 
  340. 1X> drop
  341. X> 
  342. X> % sooner or later we wish to redo definitions or discard
  343. X> % them.  The word FORGET is used:
  344. X> 
  345. X> 'IS  FORGET
  346. X> 
  347. X> % testing:
  348. X> 
  349. X> TOP10
  350. 25280   X
  351. 25264   Z
  352. 25248   Y
  353. 25234   1K
  354. 25132   FINISH
  355. 25066   WRITE
  356. 25020   READ
  357. 24990   1READ
  358. 24930   DELETES
  359. 24912   REPLACE
  360. 1X> 
  361. 1X> drop
  362. X> 
  363. X> % FORGET not only discards the chosen word (here "IS"), but
  364. X> % also all more recent definitions.  This behavior is
  365. X> % following a long tradition that was in FORTH and in
  366. X> % STOIC.  At times it is convenient, at times inconvenient.
  367. X> % Consider whether XYZ? would have been useful after IS was
  368. X> % discarded.
  369. X> 
  370. X> % FORGET is a word that causes designers of this kind of
  371. X> % language sleepless nights.
  372. X> 
  373. X> % This session has lasted long enough.... Why don't you
  374. X> % continue experimenting and learning PISTOL more
  375. X> % systematically by reading PISTOL.DOC?
  376. X> 
  377. X> bye
  378. 80   X
  379. 25264   Z
  380. 25248   Y
  381. 25234   1K
  382. 25132   FINISH
  383. 1X> 
  384. 1X> drop
  385. X> 
  386. X> % sooner